let vue = new Vue({
    el: "#department",
    data(){
        return{
            //基本分页信息
            page:1,
            pageSize:10,
            pageCount: 20,
            totalData: 999,
            startRow:5,
            searchTitle:'部门',
            switchStatus:false,
            searchContent:'',
            flag : true,
            //查询为空提示信息区域显示
            isTip:false,
            //查询选项名称
            //查询为空提示信息
            tip:'',
            //职位信息
            departments:[],
            //部门信息
            positions: [],
            //新增表单项
            addForm:{
                addPositionId:'',
                addDepId: '部门',
                addDepName: '',
                addPositionName: '职位',
                addStatus:1
            },
            //修改表单项
            updForm:{
                updId : '',
                updDepId: '',
                updDepName: '',
                updPositionName: '',
                updStatus:''
            },
            //删除信息
            deleteMsg:{
                positionId:1,
                departmentName:'',
                positionName:''
            }
        }
    },

    methods:{

        reset(){
            // this.
        },

        //获取部门信息
        getDepartment(){
            axios.get('/dep/getDepartment').then(res=>{
                this.departments = res.data.msg;
            })
        },

        //打开修改模态框并填充信息
        toUpd(position){
            this.updForm.updId = position.positionId;
            this.updForm.updDepName = position.department.name;
            this.updForm.updDepId = position.department.dep_id;
            this.updForm.updPositionName = position.positionName;
            this.updForm.updStatus = position.status;
        },

        //切换筛选状态
        switchChange(){
            this.flag = true;
            this.departments = [],
            this.switchStatus = ! this.switchStatus;
            this.page = 1;
            this.getList(this.page);
        },

        //修改职位状态
        changeStatus(id,status){
            if (status==0){
                status =1;
            }else {
                status = 0;
            }
            axios.post('/position/updStatus',{
                positionId : id,
                status :status,
            }).then(res=>{
                //修改成功
                if (res.data.msg){
                    //筛选状态为true
                    this.flag = true;
                    this.getList(this.page);
                }else {
                    lightyear.notify(res.data.msg,"danger","500","mdi mdi-checkbox-marked-close")
                }
            })
        },

        //添加职位信息
        addMessage(){
            this.flag = true;
            axios.post('/position/add',{
                positionName : this.addForm.addPositionName,
                depId : this.addForm.addDepId,
                status : this.addForm.addStatus
            }).then(res=>{
                //修改信息成功
                if(res.data.status){
                    //获取页尾码
                    this.getList(Math.ceil(this.totalData/this.pageSize));
                    lightyear.notify(res.data.msg,"success","500","mdi mdi-checkbox-marked-circle")
                }else {
                    lightyear.notify(res.data.msg,"danger","500","mdi mdi-checkbox-marked-close")
                }
            })
            this.addForm = {addStatus:1,addDepId: '部门',addPositionName: '职位'};
        },

        //填充删除信息
        toDelete(position){
            this.deleteMsg.positionId = position.positionId;
            this.deleteMsg.positionName = position.positionName;
            this.deleteMsg.departmentName = position.department.name;
        },

        //删除信息
        deleteMessage(){
            axios.get('/position/delete',{
                params:{
                    positionId : this.deleteMsg.positionId,
                }
            }).then(res=>{
                this.flag = true;
                this.getList(this.page);
                lightyear.notify(res.data.msg,"success","500","mdi mdi-checkbox-marked-circle")
            })
        },

        //修改职位信息
        updMessage(){
            axios.post('/position/update',{
                positionId : this.updForm.updId,
                positionName : this.updForm.updPositionName,
                status : this.updForm.updStatus,
            }).then(res=>{
                if(res.data.status){
                    this.getList(this.page);
                }else {
                    lightyear.notify(res.data.msg,"success","500","mdi mdi-checkbox-marked-close")
                }
            })
        },

        //搜索栏查询
        search(){
            this.flag = true;
            this.getList(1);
        },

        //刷新职位信息
        getList(page){
            let status;
            if (this.switchStatus){
                status = 1;
            }else {
                status = 0;
            }
            axios.get("/position/getList",{
                params:{
                    page : page,
                    pageSize : this.pageSize,
                    positionName : this.searchContent,
                    status : status
                }
            }).then(res=>{
                this.positions = res.data.msg.list;
                this.flashPage(res.data.msg);
                if (this.flag){
                    this.paginationFlash();
                    this.flag = false;
                }
                if (res.data.msg.list==''||res.data.msg.list==null){
                    this.tip = '很抱歉，未找到您需要的信息~'
                }else {
                    this.tip = ''
                }
            })
        },

        //刷新分页信息
        paginationFlash(){
            $('#pagination').pagination({
                current: vue.$data.page,
                pageCount: this.pageCount,//总页码
                coping: true,
                isHide: true,
                callback: function (e){
                    vue.$data.page = e.getCurrent();
                }
            });
        },

        //分页信息刷新
        flashPage(data){
            this.tip='';
            this.pageCount = data.pages;
            this.pageSize = data.pageSize;
            this.totalData = data.total;
            this.startRow = data.startRow;
        }
    },
    created(){
        this.getList(this.page);
        this.getDepartment();
    },
    watch: {
        page: function (val) {
            this.getList(val);
        },
    }
});
